package znbkChangeNote.presenter;

import com.example.lancooznbk.presenter.presenter_interface.IBaseReviewPresenter;
import com.example.lancooznbk.view.base.ILoadDataPresenter;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;

import znbkChangeNote.SaveNoteEntityModel;
import znbkChangeNote.api.SaveNoteApi;
import znbkChangeNote.entity.SaveNoteEntity;
import znbkTestPaperBj.ZnbkPaperBjInfor;
import znbkTestPaperBj.activity.NotePaperActivity;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTopicType.NoteDetailActivity;

/**
 * Created by Administrator on 2018\3\14 0014.
 */

public class SaveNotePresenter implements ILoadDataPresenter {

    private ILoadPaperView view;
    private int ModelFrom;
    private SaveNoteEntity SaveNoteEntity;
    private boolean isFromExit = false;

    public SaveNotePresenter(ILoadPaperView view, int modelFrom) {
        this.view = (ILoadPaperView) view;
        this.ModelFrom = modelFrom;
    }

    public SaveNotePresenter(ILoadPaperView view,int modelFrom,SaveNoteEntity saveNoteEntity) {
        this.view = (ILoadPaperView) view;
        this.ModelFrom = modelFrom;
        this.SaveNoteEntity =saveNoteEntity;
    }

    @Override
    public void getData(boolean isShowLoading) {

        HttpOnNextListener<String> httpOnNextListener = new HttpOnNextListener<String>(){

            @Override
            public void onNext(String saveNoteResult) {
                if(saveNoteResult.equals("true")){
//                    ToastUtils.showShort("数据保存成功!");
                }else{
                    ToastUtils.showShort("数据保存失败! 错误信息"+saveNoteResult);
                }
            }
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                ToastUtils.showShort("数据保存失败!");
            }
        };


        SaveNoteApi api = null;

        if(ModelFrom== ZnbkPaperDef.FROM_NOTE) {
            api = new SaveNoteApi(httpOnNextListener, (NotePaperActivity) view);
            api.setmSaveNoteEntity(SaveNoteEntityModel.getSaveNotesEntity(SaveNoteEntityModel.getNoteBean(UserInfo.StuId, ZnbkPaperBjInfor.ResType,
                    UserInfo.SubjectId,ZnbkPaperBjInfor.NoteID,UserInfo.StageNo,ZnbkPaperBjInfor.QuesID),ZnbkPaperBjInfor.Title,ZnbkPaperBjInfor.Content));
        }else if(ModelFrom==ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING||
                ModelFrom==ZnbkPaperDef.FROM_TRAINING_DIFFICULT_PROBLEMS||
                ModelFrom==ZnbkPaperDef.FROM_SYSTEM_REVIEW||
                ModelFrom==ZnbkPaperDef.FROM_WEEK_ASCENSION||
                ModelFrom==ZnbkPaperDef.FROM_HISTORY_RECOMMEND_QUESTION||
                ModelFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION_SPL||
                ModelFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION){
            api = new SaveNoteApi(httpOnNextListener, (NoteDetailActivity) view);
            api.setmSaveNoteEntity(SaveNoteEntity);
        }


        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getMoreData() {
        getData(false);
    }

    public void getData(boolean isShowing ,boolean isFromExit){
        this.isFromExit = isFromExit;
        getData(isShowing);
    }

}